package com.czp;

import java.io.IOException;

import java.util.Date;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

import Utils.JsonUtils;

@WebServlet("/inform")
@MultipartConfig
public class orderFormSevlet extends HttpServlet {
	private static final long serialVersionUID = 6915020476106513112L;
	private orderFormSevice service = new orderFormSevice();
	private JsonUtils jsonUtils = new JsonUtils();
	private JSONObject json = new JSONObject();
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getMethod();
		System.out.println("service---->"+methodName);
		if ("POST".equals(methodName)) {
			doPost(request, response);
		} else if ("GET".equals(methodName)) {
			doGet(request, response);
		}
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String methodName = request.getMethod();
		System.out.println("methodName-----"+methodName);
		String method = request.getParameter("method");
		if ("querys".equals(method)) {
			querys(request, response);
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}

	private void querys(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		String num  = request.getParameter("number");
		int number = Integer.parseInt(num);
		// url上的值
		String hot_id  = request.getParameter("hotel_id");
		int hotel_id = Integer.parseInt(hot_id);
		System.out.println("hotel_id="+hotel_id);
		
		String startDate  = request.getParameter("startDate");
		String endDate  = request.getParameter("endDate");
		String sumPrice  = request.getParameter("sumPrice");
		// 计算天数单价要值
		int sump = Integer.parseInt(sumPrice);
		
		// 旅客信息1
		String cname2  = request.getParameter("cname2");
		String cname1  = request.getParameter("cname1");
		String gender1  = request.getParameter("gender1");
		String birthDay1  = request.getParameter("birthDay1");
		String IDtype1  = request.getParameter("IDtype1");
		String idcard1  = request.getParameter("idcard1");
		String phone1  = request.getParameter("phone1");
		
		// 联系人
		String ContactName  = request.getParameter("ContactName");
		String phone3  = request.getParameter("phone3");
		String email  = request.getParameter("email");
		String weixing  = request.getParameter("weixing");
		
		// 联系人存储方法
		Long p_id = service.saveContact(ContactName,phone3,email,weixing);
		int person_id = p_id.intValue();
		if(person_id!=0) {
			System.out.println("联系人存储成功....");
		}
		
		int c1 = service.saveCustomer(cname2,cname1,gender1,birthDay1,IDtype1,idcard1,phone1,person_id);
		if(c1!=0) {
			System.out.println("旅客信息1保存成功....");
		}
		// 旅客信息2
		if(number==2) {
			String cname4  = request.getParameter("cname4");
			String cname3  = request.getParameter("cname3");
			String gender2  = request.getParameter("gender2");
			String birthDay2  = request.getParameter("birthDay2");
			String IDtype2  = request.getParameter("IDtype2");
			String idcard2  = request.getParameter("idcard2");
			String phone2  = request.getParameter("phone2");
			int c2 = service.saveCustomer(cname4,cname3,gender2,birthDay2,IDtype2,idcard2,phone2,person_id);
			if(c2!=0) {
				System.out.println("旅客信息2保存成功....");
			}
		}
		// 日期大小计算
		int day_number = (int) service.dayDiff(startDate, endDate, "yyyy-MM-dd");
		int night_day = day_number-1;
		double price_day = sump/day_number;
		int buy_number = number;
		int bed_number = 1;
		String sum_price = sumPrice;
		
		// Price保存
		Long price = service.savePrice(startDate,endDate,day_number,night_day,price_day,buy_number,bed_number,sum_price,person_id,hotel_id);
		if(price!=0) {
			System.out.println("---->"+price);
			System.out.println("Price表保存成功....");
		}
		// 生成的订单编号
		String pay_number = getrandomNum();
		System.out.println("订单编号------->"+pay_number);
		
		json.fluentPut("price", price);
		json.fluentPut("pay_number", pay_number);
		jsonUtils.send(response, json);
	}
	
	@SuppressWarnings("deprecation")
	public String getrandomNum() {
		Date vNow = new Date();
		String sNow = "";
		sNow += (vNow.getYear()-100);
		sNow += (vNow.getMonth() + 1);
		sNow += vNow.getDate();
		sNow += vNow.getHours();
		sNow += vNow.getMinutes();
		sNow += vNow.getSeconds();
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		sNow += new Random().nextInt(10);
		return sNow;
	}
	
	
	
}